package com.mobiftpcms.dao.hibernate;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.mobiftpcms.dao.StationDao;
import com.mobiftpcms.model.Station;

public class StationDaoHibernate implements StationDao {
	
	private Log log = LogFactory.getLog(StationDaoHibernate.class);
    private HibernateTemplate hibernateTemplate;
    
    /**
     * Initialize StationDaoHibernate with Hibernate SessionFactory.
     * @param sessionFactory
     */
    @Autowired
    public StationDaoHibernate(final SessionFactory sessionFactory) {
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    @SuppressWarnings("unchecked")
	public List<Station> getStations() {
		log.debug("Retrieving stations...");
		return hibernateTemplate.find("from Station");
	}

    
	public Station saveStation(Station station) {
		hibernateTemplate.saveOrUpdate(station);
		hibernateTemplate.flush();
		return station;
	}

	public Station deleteStation(Station station) {
		hibernateTemplate.delete(station);
		return station;
	}
	
	public Station getStationByName(String name) {
		List<Station> stations = hibernateTemplate.find("from Station s where s.name=?",name);
		Station station = null;
		if(!stations.isEmpty()) {
			station = (Station) stations.get(0);
		}
		return station;
	}

}
